import {request1} from "../../public/js/request1"

// pages/goods/goods.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //储存商品详情数据
    goodsdata:[],
    //存储收藏数据
    collection:[],
    //收藏是否被选中
    collection_index:0
  },

  //购物栏的数据
  cartdatas:{
  },

  c_click(e){
    console.log(this.data.goodsdata.pics);
    let urls = this.data.goodsdata.pics.map(item => item.pics_mid)
    let index = e.currentTarget.dataset.index
    wx.previewImage({
      urls,
      current:urls[index]
    })
  },

  cart_click(e){
    console.log(this.cartdatas);
    let cartlist = wx.getStorageSync('cart') || []
    console.log(cartlist);
    let iscun = cartlist.findIndex(item => item.goods_id == this.cartdatas.goods_id)

    if(iscun !== -1){
      //购物车存在
      cartlist[iscun].count += 1
    }else{
      //购物车不存在
      this.cartdatas.count = 1
      cartlist.push(this.cartdatas)
    }

    wx.setStorageSync('cart', cartlist)

    wx.showToast({
      title: '加入购物车',
      icon:"success",
      mask:true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);

    //请求购物车详情数据
    request1({
      url:"https://api-hmugo-web.itheima.net/api/public/v1/goods/detail?goods_id=" + options.id
    }).then(result => {
      console.log(result.data.message);

      this.is_collections(result.data.message.goods_id)
      //goods_big_logo,
    //goods_name,
    //goods_price
      this.cartdatas.goods_big_logo = result.data.message.goods_big_logo
      this.cartdatas.goods_name = result.data.message.goods_name
      this.cartdatas.goods_price = result.data.message.goods_price
      this.cartdatas.goods_id = result.data.message.goods_id
      this.cartdatas.is_promote = result.data.message.is_promote
      this.cartdatas.is_collection = true

      this.setData({
        goodsdata:result.data.message
      })
    })
  },

  //点击收藏
  collection(e){
    const collection = wx.getStorageSync('collection') || []
    console.log(this.cartdatas.goods_id);
    let isture = collection.findIndex(item => item.goods_id === this.cartdatas.goods_id)

    //如果等于-1就说明不存在
    if(isture === -1){
      collection.push(this.cartdatas)
      wx.showToast({
        title: '加入收藏',
        icon:"success",
        mask:true
      })
    }else{
      console.log('索引',isture);
      collection.splice(isture,1)
      wx.showToast({
        title: '取消收藏',
        icon:"error",
        mask:true
      })
    }

    wx.setStorageSync('collection', collection)

    this.is_collections()
  },
  
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.is_collections()
  },

  //封装收藏是否选中
  is_collections(current_index){
    let collection = wx.getStorageSync('collection')
    let isture = collection.findIndex(item => item.goods_id == current_index)

    let collection_index = isture
    this.setData({
      collection_index,
      collection
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})